//
//  ZYImageCacheTask.m
//  ZYWebImage
//
//  Created by wangzhipan on 2025/3/18.
//

#import "ZYImageCacheTask.h"
#import "ZYWebImageConst.h"

@interface ZYImageCacheTask()
@property (nonatomic, strong, readwrite ,nullable) NSString *key;
@property (nonatomic, assign, readwrite, getter=isCancelled) BOOL cancelled;
@property (nonatomic, copy, nullable) ZYImageCacheQueryCompletionBlock completed;
@end

@implementation ZYImageCacheTask

- (instancetype)initWithKey:(NSString *)key
                  completed:(ZYImageCacheQueryCompletionBlock)completed
{
    if (self = [super init]) {
        self.key = key;
        self.completed = completed;
    }
    return self;
}

- (void)cancel
{
    @synchronized (self) {
        if (self.cancelled) {
            return;
        }
        self.cancelled = true;
        ZYImageCacheQueryCompletionBlock doBlock = self.completed;
        self.completed = nil;
        if (doBlock) {
            SafeAsyncMainThread(^{
                doBlock(nil, nil, ZYImageCacheTypeNone);
            });
        }
    }
}
@end
